<template>
	<view class="pb-100">
		<empty text="暂无地址" v-if="list.length==0"></empty>

		<uni-swipe-action>
			<uni-swipe-action-item :right-options="options" @click="del(item.id)" v-for="(item, index) in list" :key="index">
				<view class="item bb pd-26">
					<view class="top" @click="selectAdd(item.id)">
						<uni-badge size="small" text="默" absolute="rightBottom" type="error" v-if="item.is_default"></uni-badge>
						<view class="name">{{ item.name }}</view>
						<view class="phone plr-24">
							<text>{{ item.mobile }}</text>
							<text class="type">{{typeText[item.type] ? typeText[item.type] : '其他'}}</text>
						</view>
						<view class="tag" v-if="item.status==1">
							<text :class="{red:item.status==1}">{{ item.status == 1 ? '默认' : '' }}</text>
						</view>
					</view>
					<view class="bottom">
						<view @click="selectAdd(item.id)">{{ item.address }}</view>
						<u-icon @click="toAdd(item.id)" name="edit-pen" :size="40" color="#999999"></u-icon>
					</view>
				</view>
			</uni-swipe-action-item>
		</uni-swipe-action>
		<view class="pd-30 fixed-bottom">
			<u-button type="warning" @click="toAdd(null)">
				<u-icon name="plus" color="#ffffff" class="icon" :size="30"></u-icon>
				添加服务地址
			</u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				goods_id: '',
				is_return: false,
				lat: '',
				lng: '',
				typeText: {'1': '住宅', '2': '商铺', '3': '写字楼', '4': '工厂'},
				options: [{
					text: '删除地址',
					style: {
						backgroundColor: '#dd524d'
					}
				}]
			};
		},
		onLoad(option) {
			this.is_return = option.is_return ? true : false;
			// uni.getLocation({
			// 	success: (data) => {
			// 		this.lat = data.latitude;
			// 		this.lng = data.longitude;
			// 	}
			// });
		},
		onShow() {
			this.init();
		},
		methods: {
			init() {
				this.$http('/api/address/index').then(data => {
					this.list = data
				})
			},
			toAdd(id) {
				let url = '/pages/my/address/add' + (id ? `?id=${id}` : `?lat=${this.lat}&lng=${this.lng}`);
				uni.navigateTo({
					url: url
				});
			},
			selectAdd(id) {
				if (this.is_return) {
					uni.$emit('address_id', id)
					uni.navigateBack({
						delta: 1
					});
				}
			},
			del(id) {
				this.$http('/api/address/del', {
					id: id,
				}, "POST").then(data => {
					uni.showToast({
						title: '删除成功'
					});
					this.init();
				})
			}
		}
	};
</script>

<style lang="scss" scoped>
	.item {
		width: 100%;

		.top {
			display: flex;
			font-weight: bold;
			font-size: 34 rpx;

			.phone {
				margin-left: 60 rpx;
				
				.type {
					margin-left: 5px;
					font-size: 10px;
					padding: 2px;
					color: #F5CC89;
					border: 1px solid #F5CC89;
					border-radius: 4px;
				}
			}

			.tag {
				display: flex;
				font-weight: normal;
				align-items: center;

				text {
					display: block;
					width: 60 rpx;
					height: 34 rpx;
					line-height: 34 rpx;
					color: #ffffff;
					font-size: 20 rpx;
					border-radius: 6 rpx;
					text-align: center;
					margin-left: 30 rpx;
					background-color: rgb(49, 145, 253);
				}

				.red {
					background-color: red
				}
			}
		}

		.bottom {
			display: flex;
			margin-top: 20 rpx;
			font-size: 28 rpx;
			justify-content: space-between;
			color: #999999;
		}
	}
</style>
